home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests.old / client / client.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-02  |  4.4 KB  |  166 lines

  1. /* 
  2.  * client.c --
  3.  *
  4.  *    Test program for printf server.
  5.  *
  6.  * Copyright 1991 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that this copyright
  10.  * notice appears in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /user6/kupfer/spriteserver/src/client/RCS/client.c,v 1.6 91/08/30 16:06:00 kupfer Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <ctype.h>
  21. #include <mach.h>
  22. #include <mach_error.h>
  23. #include <status.h>
  24. #include <stdio.h>
  25. #include <user/proc.h>
  26. #include "spriteSrv.h"
  27.  
  28. #define SHARED_ERROR_REGION    1
  29.  
  30. mach_port_t serverPort;        /* port for making Sprite requests */
  31.  
  32. /* Forward references */
  33.  
  34. static int GetLength _ARGS_((char *fileName));
  35. static void MakeFile _ARGS_((char *fileName));
  36. static void MapFile _ARGS_((char *fileName, boolean_t readOnly,
  37.                 int length, Address *startAddrPtr));
  38. static void PrintBuffer _ARGS_((char *fileName, char *bufPtr, int length));
  39. static void WriteToBuffer _ARGS_((char *fileName, char *bufPtr, int length));
  40.  
  41. main()
  42. {
  43.     kern_return_t kernStatus;
  44. #ifdef SHARED_ERROR_REGION
  45.     int *errorPtr = (int *)PROC_SHARED_REGION_START;
  46. #endif
  47.     char *fromName = "testInput"; /* name of file to copy from */
  48.     char *fromBuffer;        /* mapped "from" file */
  49.     char *toName = "testOutput"; /* name of file to copy to */
  50.     char *toBuffer;        /* mapped "to" file */
  51.     int fileLength;
  52.  
  53.     kernStatus = task_get_bootstrap_port(mach_task_self(), &serverPort);
  54.     if (kernStatus != KERN_SUCCESS) {
  55. #if SHARED_ERROR_REGION
  56.     *errorPtr = kernStatus;
  57. #endif
  58.     thread_suspend(mach_thread_self());    
  59.     }
  60.  
  61.     fileLength = GetLength(fromName);
  62. #if 0
  63.     if (fileLength < 0) {
  64.     Test_PutMessage(serverPort, "bailing out.\n");
  65.     goto bailOut;
  66.     }
  67. #endif
  68.  
  69.     MapFile(fromName, TRUE, fileLength, &fromBuffer);
  70.     MapFile(toName, FALSE, fileLength, &toBuffer);
  71.  
  72.     if (fromBuffer != 0 && toBuffer != 0) {
  73.     bcopy(fromBuffer, toBuffer, fileLength);
  74.     }
  75.  
  76.  bailOut:
  77.     Sys_Shutdown(serverPort);
  78. }
  79.  
  80.  
  81. /*
  82.  *----------------------------------------------------------------------
  83.  *
  84.  * MapFile --
  85.  *
  86.  *    Map the named file into our address space.
  87.  *
  88.  * Results:
  89.  *    Fills in the starting location, which is set to 0 
  90.  *    if there was a problem.
  91.  *
  92.  * Side effects:
  93.  *    None.
  94.  *
  95.  *----------------------------------------------------------------------
  96.  */
  97.  
  98. static void
  99. MapFile(fileName, readOnly, length, startAddrPtr)
  100.     char *fileName;        /* name of file to map */
  101.     boolean_t readOnly;        /* map read-only or read-write? */
  102.     int length;            /* number of bytes to map */
  103.     Address *startAddrPtr;    /* OUT: where the file was mapped to */
  104. {
  105.     kern_return_t kernStatus;
  106.     ReturnStatus status;
  107.  
  108.     kernStatus = Vm_MapFileStub(serverPort, fileName, strlen(fileName)+1,
  109.                 readOnly, 0, length, &status, startAddrPtr);
  110.     if (kernStatus != KERN_SUCCESS) {
  111.     Test_PutMessage(serverPort, "Couldn't map file: ");
  112.     Test_PutMessage(serverPort, mach_error_string(kernStatus));
  113.     Test_PutMessage(serverPort, "\n");
  114.     *startAddrPtr = 0;
  115.     } else if (status != SUCCESS) {
  116.     Test_PutMessage(serverPort, "Couldn't map file: ");
  117.     Test_PutMessage(serverPort, Stat_GetMsg(status));
  118.     Test_PutMessage(serverPort, "\n");
  119.     *startAddrPtr = 0;
  120.     }
  121. }
  122.  
  123.  
  124. /*
  125.  *----------------------------------------------------------------------
  126.  *
  127.  * GetLength --
  128.  *
  129.  *    Get the length of a file.
  130.  *
  131.  * Results:
  132.  *    Returns the length of the file, in bytes.  Returns -1 if there 
  133.  *    was an error.
  134.  *
  135.  * Side effects:
  136.  *    None.
  137.  *
  138.  *----------------------------------------------------------------------
  139.  */
  140.  
  141. static int
  142. GetLength(fileName)
  143.     char *fileName;
  144. {
  145.     ReturnStatus status;
  146.     int length;
  147.     kern_return_t kernStatus;
  148.  
  149.     kernStatus = TempFs_LengthStub(serverPort, fileName, strlen(fileName)+1,
  150.                    &status, &length);
  151.     if (kernStatus != KERN_SUCCESS) {
  152.     Test_PutMessage(serverPort, "Couldn't get file length: ");
  153.     Test_PutMessage(serverPort, mach_error_string(kernStatus));
  154.     Test_PutMessage(serverPort, "\n");
  155.     return -1;
  156.     }
  157.     if (status != SUCCESS) {
  158.     Test_PutMessage(serverPort, "Couldn't get file length: ");
  159.     Test_PutMessage(serverPort, Stat_GetMsg(status));
  160.     Test_PutMessage(serverPort, "\n");
  161.     return -1;
  162.     }
  163.  
  164.     return length;
  165. }
  166.